//
//  ServiceDetailChecklistTableViewCell.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/24.
//

import UIKit
import SnapKit
class ServiceDetailChecklistTableViewCell: TableViewCell {
    
    lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    lazy var typeImageView: UIImageView = {
        return UIImageView()
    }()
    lazy var titleLabel: BaseLabel = {
        let label = ContentLabel(R.font.interSemiBold(size: 16),hexStr: "#292D32FF")
        label.numberOfLines = 2
        return label
    }()
    
    lazy var subTitleLabel = ContentLabel(R.font.interRegular(size: 13), hexStr: "#292D32B2")
    lazy var resultLabel: BaseLabel = {
        let label = ContentLabel(R.font.interMedium(size: 14),hexStr: "#63666AFF")
        label.numberOfLines = 0
        return label
    }()
    lazy var moreImageView: UIImageView = {
        return UIImageView(image: UIImage(named: "pvdlist_icon_more"))
    }()
    
    lazy var booleanCheckBoxImageView: UIImageView = {
        return UIImageView()
    }()
    lazy var isRequiredImageView: UIImageView = {
        return UIImageView(image: UIImage(named: "checkListCellStar"))
    }()
    lazy var localDataFlagImageView: UIImageView = {
        return UIImageView(image: UIImage(named: "uploadImageFailed"))
    }()
    
    var booleanCheckBoxImageViewWidthConstant: Constraint?
   
    override func setUpviews(){
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.contentView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.selectionStyle = .none
        //
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(70)
            make.top.bottom.equalToSuperview()
        }
        //type image view
        backView.addSubview(typeImageView)
        typeImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
        }
        //local image
        backView.addSubview(localDataFlagImageView)
        localDataFlagImageView.snp.makeConstraints { make in
            make.left.equalTo(typeImageView.snp.right).offset(-8)
            make.bottom.equalTo(typeImageView.snp.bottom).offset(3)
            make.width.height.equalTo(13)
        }
        //contentBackView
        let contentBackView = UIView()
        contentBackView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        backView.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.left.equalTo(typeImageView.snp.right).offset(12)
            make.centerY.equalTo(typeImageView)
            make.height.width.greaterThanOrEqualTo(0)
            
        }
        //title label
        contentBackView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)

        }
        subTitleLabel.numberOfLines = 0
        contentBackView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.equalTo(titleLabel)
            make.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()

        }
        //isRequiredImageView
        backView.addSubview(isRequiredImageView)
        isRequiredImageView.snp.makeConstraints { make in
            make.top.equalTo(typeImageView).offset(6)
            make.left.equalTo(typeImageView.snp.right).offset(4)
            make.height.width.equalTo(7)
        }
        //more image view
        moreImageView.contentMode = .center
        backView.addSubview(moreImageView)
        moreImageView.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        //
        backView.addSubview(booleanCheckBoxImageView)
        booleanCheckBoxImageView.snp.makeConstraints { make in
            make.centerX.equalTo(moreImageView)
            make.height.equalTo(24)
            self.booleanCheckBoxImageViewWidthConstant = make.width.equalTo(0).constraint
            make.centerY.equalToSuperview()
        }
        //result label
        //抗压缩,抗拉伸能力最强，不会被压缩或拉伸
        resultLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        resultLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        backView.addSubview(resultLabel)
        resultLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(5)
            make.right.equalTo(moreImageView.snp.left)
            make.height.equalTo(40)
            make.width.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
        isRequiredImageView.isHidden = true
        localDataFlagImageView.isHidden = true
        booleanCheckBoxImageView.isHidden = true
    }
    
    func setData(_ serverModel: PVDChecklistModel,lastRowFlag: Bool) {
        let type = serverModel.type
        if lastRowFlag{//最后一行
            self.backView.drawCorner(byRoundingCorners: [.bottomLeft,.bottomRight])
        }
        //2.check if checklist is requird or optional
        if serverModel.is_required == "true" {
            self.isRequiredImageView.isHidden = false
        }else {
            self.isRequiredImageView.isHidden = true
        }
        self.localDataFlagImageView.isHidden = true
        self.booleanCheckBoxImageView.isHidden = true
        self.titleLabel.text = serverModel.title
        self.typeImageView.image = UIImage(named:"service_bidjob_checklist_" + type + "_enabled")
        
        switch serverModel.type {
        case ChecklistType.Image.rawValue:
            
            self.moreImageView.image = UIImage(named: "more_gray_icon")
            self.moreImageView.isHidden = false
            self.booleanCheckBoxImageView.image = nil
            self.booleanCheckBoxImageViewWidthConstant?.update(offset: 0)
            self.resultLabel.isHidden = false
            self.localDataFlagImageView.isHidden = !serverModel.haveLocalDataFlag
            
            let imageCnt: Int = serverModel.images.count
            if imageCnt > 0 {
                self.resultLabel.text = "#\(imageCnt)"
                self.resultLabel.textColor = UIColor(hexStr: "#63666A")
            }else{
                self.resultLabel.text = ""
            }
        case ChecklistType.Text.rawValue:
            self.moreImageView.isHidden = false
            self.booleanCheckBoxImageView.image = nil
            self.booleanCheckBoxImageViewWidthConstant?.update(offset: 0)
            self.resultLabel.isHidden = false
            self.resultLabel.text = ""
            self.localDataFlagImageView.isHidden = !serverModel.haveLocalDataFlag
            if serverModel.answer != "" {
                self.resultLabel.text = R.string.localizable.done()
                self.resultLabel.textColor = UIColor(hexStr: "#379657")
            }
        case ChecklistType.Bool.rawValue:
            self.moreImageView.isHidden = true
            self.booleanCheckBoxImageView.isHidden = false
            self.booleanCheckBoxImageViewWidthConstant?.update(offset: 24)
            self.resultLabel.isHidden = true
            self.localDataFlagImageView.isHidden = !serverModel.haveLocalDataFlag
            if serverModel.answer == "true" {
                self.booleanCheckBoxImageView.image = UIImage(named: "PVDDetail_checklist_btn_check_sel")
            }else{
                self.booleanCheckBoxImageView.image = UIImage(named: "PVDDetail_checklist_btn_check")
            }
        default:
            break
        }
        
    }
    
}
